package com.qdu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //指定验证和授权用户使用的UserDetailsService对象是哪个
        //并且指定使用的密码加密器
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(encoder);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**","/js/**","/img/**","/static/img/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
//                .antMatchers("/user/**").hasRole("user")
//                .antMatchers("/admin/**").hasAnyRole("admin","sadmin")
//                .antMatchers("/sadmin/**").hasAnyRole("sadmin")
//                .anyRequest().authenticated()
//                .and()
//                .formLogin().loginPage("")
//                .defaultSuccessUrl("/index",true)
//                .failureUrl("/login_failed")
//                .and()
//                .rememberMe()
//                .rememberMeCookieName("remember")
//                .rememberMeParameter("rememberMe")
//                .tokenValiditySeconds(30*60)
                .and()
//                .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
                .csrf().disable()
//                .and()
                .httpBasic();
    }

//    @Bean
//    public BCryptPasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }
}
